﻿using System;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class SysManage_RoleConfig : UserPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SetPager();
            Page_PreBind();
            User_PreBind();
        }
    }

    private void Page_PreBind()
    {
        string sqlstr = "SELECT data_code,data_name FROM u_pub_data_t WHERE data_type='menu_page' AND if_valid = 'Y' ORDER BY order_num";
        PageList.DataSource = OracleHelper.ExecuteDataSet(sqlstr, CommandType.Text, null);
        PageList.DataTextField = "data_code";
        PageList.DataValueField = "data_code";
        PageList.DataBind();
    }

    private void User_PreBind()
    {
        string sqlstr = "select login_name,real_name from u_user_info_t where if_valid = 'Y' order by login_name";
        UserList.DataSource = OracleHelper.ExecuteDataSet(sqlstr, CommandType.Text, null);
        UserList.DataTextField = "real_name";
        UserList.DataValueField = "login_name";
        UserList.DataBind();
    }

    private void SetPager()
    {
        PagedDataSource pds = new PagedDataSource();
        DataSet ds = GetRole();
        RolePager.RecordCount = ds.Tables[0].Rows.Count;
        pds.DataSource = ds.Tables[0].DefaultView;
        pds.AllowPaging = true;
        pds.PageSize = RolePager.PageSize;
        pds.CurrentPageIndex = RolePager.CurrentPageIndex - 1;
        RoleList.DataSource = pds;
        RoleList.DataBind();
    }

    private DataSet GetRole()
    {
        string sqlstr = @"select data_name AS role_name,data_code AS role_code
                                           ,count(distinct t2.page_code) AS page_num,count(distinct t3.login_name) AS user_num 
                                      from u_pub_data_t t1
                                      left join u_role_page_t t2
                                        on t1.data_code = t2.role_code
                                       and t2.if_valid = 'Y'
                                      left join u_user_role_t t3
                                        on t1.data_code = t3.role_code
                                       and t3.if_valid = 'Y'
                                     WHERE t1.data_type = 'role_list'
                                         AND t1.if_valid = 'Y' ";
        if (Role_I.Text.Trim().Length > 0)
        {
            sqlstr = sqlstr + " AND t1.data_name like '%" + Tools.ReplaceString(Role_I.Text.Trim(), 200) + "%' ";
        }
        sqlstr = sqlstr + " GROUP BY data_name,data_code";
        return OracleHelper.ExecuteDataSet(sqlstr, CommandType.Text, null);
    }

    protected void QueryBtn_Click(object sender, EventArgs e)
    {
        SetPager();
    }

    protected void RoleList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "PageConfig")
        {
            PageSet_Bind(e.CommandArgument.ToString());
            Role_Name_R.InnerText = GetPubDataName("role_list", e.CommandArgument.ToString());
            Role_Code_R.Value = e.CommandArgument.ToString();
            Page.ClientScript.RegisterStartupScript(this.GetType(), "open"
                , "$(function() {$('#PageSetDiv').dialog('open');})", true);
        }
        else if (e.CommandName == "UserConfig")
        {
            UserSet_Bind(e.CommandArgument.ToString());
            Role_Name_U.InnerText = GetPubDataName("role_list", e.CommandArgument.ToString());
            Role_Code_U.Value = e.CommandArgument.ToString();
            Page.ClientScript.RegisterStartupScript(this.GetType(), "open"
                , "$(function() {$('#UserSetDiv').dialog('open');})", true);
        }
    }

    private void PageSet_Bind(string arg)
    {
        string sqlstr = @"SELECT data_code FROM u_pub_data_t WHERE data_type='menu_page' AND if_valid = 'Y'  
                                AND data_code IN(SELECT page_code FROM u_role_page_t 
                                                            WHERE role_code =:role_code AND if_valid='Y')";
        OracleParameter[] _params = {
                    new OracleParameter(":role_code",OracleType.VarChar,20)
            };
        _params[0].Value = arg;
        DataSet ds = OracleHelper.ExecuteDataSet(sqlstr, CommandType.Text, _params);
        IList<string> s = new List<string>();
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            s.Add(dr["data_code"].ToString());
        }
        foreach (ListItem c in PageList.Items)
        {
            c.Selected = s.Contains(c.Value);
        }
    }

    private void UserSet_Bind(string arg)
    {
        string sqlstr = @"select login_name from u_user_info_t
                                 where if_valid = 'Y' 
                                   and login_name IN (select login_name from u_user_role_t 
                                                       where if_valid = 'Y' and role_code = :role_code)";
        OracleParameter[] _params = {
                    new OracleParameter(":role_code",OracleType.VarChar,20)
            };
        _params[0].Value = arg;
        DataSet ds = OracleHelper.ExecuteDataSet(sqlstr, CommandType.Text, _params);
        IList<string> s = new List<string>();
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            s.Add(dr["login_name"].ToString());
        }
        foreach (ListItem c in UserList.Items)
        {
            c.Selected = s.Contains(c.Value);
        }
    }

    protected void SetPageBtn_Click(object sender, EventArgs e)
    {
        string sqlstr = "U_MODIFY_PAGE_P";
        OracleParameter[] _param = {
			new OracleParameter("V_PAGE",OracleType.VarChar,1024),
			new OracleParameter("V_ROLE",OracleType.VarChar,16),
            new OracleParameter("V_OPERATOR",OracleType.VarChar,16)			
	    };
        _param[0].Value = GetCheckString(PageList);
        _param[1].Value = Role_Code_R.Value;
        _param[2].Value = Session["LoginUser"].ToString();
        int r = OracleHelper.ExecuteNonQuery(sqlstr, CommandType.StoredProcedure, _param);
        if (r > 0)
        {
            SetPager();
            JQueryAlert("权限设置成功！");
        }
    }

    protected void RolePager_PageChanged(object sender, EventArgs e)
    {
        SetPager();
    }

    protected void UserBtn_Click(object sender, EventArgs e)
    {
        if (Role_Code_U.Value.Equals(RoleSys) && UserList.SelectedIndex == -1)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "sysinfo"
                , "$(function() {$('#UserSetDiv').dialog('open');alert('请至少配置一位系统管理员角色！');})", true);
            return;
        }
        string sqlstr = "U_MODIFY_USER_P";
        OracleParameter[] _param = {
			new OracleParameter("V_USER",OracleType.VarChar,1024),
			new OracleParameter("V_ROLE",OracleType.VarChar,16),
            new OracleParameter("V_OPERATOR",OracleType.VarChar,16)			
	    };
        _param[0].Value = GetCheckString(UserList);
        _param[1].Value = Role_Code_U.Value;
        _param[2].Value = Session["LoginUser"].ToString();
        int r = OracleHelper.ExecuteNonQuery(sqlstr, CommandType.StoredProcedure, _param);
        if (r > 0)
        {
            SetPager();
            JQueryAlert("用户设置成功！");
        }
    }

    private string GetCheckString(CheckBoxList argCk)
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i <= argCk.Items.Count - 1; i++)
        {
            if (argCk.Items[i].Selected)
            {
                sb.Append("|");
                sb.Append(argCk.Items[i].Value);
            }
        }
        if (sb.Length > 0)
            sb.Remove(0, 1);
        return sb.ToString();
    }
}
